/*
 * @Author: BanHua
 * @Date: 2021-01-15 13:38:55
 * @LastEditors: BanHua
 * @LastEditTime: 2021-01-23 00:54:26
 * @Description: file content
 */
//开始脚本
let storage = storages.create('KuGou7');
let 操作次数 = storage.get('viewDataArr')['bh_czcs'] || 30;
let 私聊话术 = storage.get('viewDataArr')['bh_slhs'] || "";
let 私聊 = storage.get('viewDataArr')['bh_siliao'] || false;
let 关注 = storage.get('viewDataArr')['bh_guanzhzu'] || false;
let 已私聊次数 = 0

console.show();

/////////////////////////////////////


this.下滑 = function () {
    var x1 = random(device.width/4,(device.width/4)*3);
    var y1 = random((device.height/4)*3.5,(device.height/4)*3.75);
    var x2 = random(device.width/4,(device.width/4)*3);
    var y2 = random((device.height/4)*0.25,(device.height/4)*0.5);
    swipe(x1, y1, x2, y2, random(500, 1000));
}

//访问用户主页函数,默认操作10个用户
this.v_visitUserHomepage = function (n_operNum) {

    let user_name_arr = storage.get("user_name_arr") || []

    while (1) {
        
        log('开始查找列表')
        let list = id("fe0").findOne()
        log('开始遍历')
        list.children().forEach((v, i) => {

            if (已私聊次数 >= 操作次数) {
                log("私聊任务完成！")
                exit();
            }

            if (v) {
                if (v.className() == "android.widget.RelativeLayout") {
                    let user_name = v.findOne(id("gdf"))
                    if (user_name) {
                        if (user_name.text().indexOf("匿名") > -1) {
                            log("第%d个用户是匿名用户", i+1)
                            
                        } else {
                            //开始存储用户的名字
                            if ( user_name_arr.indexOf(user_name.text()) > -1 ) {
                                log("【%s用户重复了】", user_name.text())
                                
                            } else {
                                //存储用户名
                                user_name_arr.push(user_name.text())
                                storage.put("user_name_arr", user_name_arr)
                                do {
                                    log("点击该用户")
                                    v.findOne(id("xj")).click()
                                    sleep(random(500, 1000))
                                } while(!id("bnj").findOnce());
            
                                do {
                                    log("点击私信")
                                    id("bnj").findOne().click()
                                    sleep(random(500, 1000))
                                } while (!id("jro").findOnce());
            
                                //读取话术并输入
                                私聊话术.split(";").forEach((t, i) => {
                                    id("jro").findOne().setText(t)
                                    sleep(random(500, 1000))
                                    id("ade").findOne().click()
                                    sleep(random(500, 1000))
                                })

                                已私聊次数++
            
                                //开始返回
                                do {
                                    log('返回')
                                    id("dmu").findOne().click()
                                    sleep(random(500, 1000))
                                } while(id("jro").findOnce());
            
                                //返回到列表
                                do {
                                    back()
                                    sleep(random(1000, 2000))
                                } while (!text("土豪榜").exists());
            
                            }
                        }
                    }
                } else if (v.className() == "android.widget.LinearLayout") {
                    text("已加载全部")
                    if (v.findOne(text("已加载全部"))) {
                        log("已加载全部")
                        return
                    }
                }
            }
        })
    
        log('开始下滑列表')
        if (id("fe0").findOne().scrollForward()) {
            log('下滑成功')
        } else {
            log('下滑失败')
            下滑()
            sleep(random(3000, 5000))
            // exit()
        }
    }

}

//兼容线程
threads.start(function(){
    //全名K歌兼容线程
    while (1) {
        if (text("关注并领取").exists() || text("送礼并抢红包").exists() || text("主播抽奖").exists()) {
            back()
        }
    }
});




(function(){
    //初始化局部变量
    loop1:
    while (1) {
        log("开始while【loop1】循环");

        //判断是否在直播界面
        text("评论").waitFor();
        text("分享").waitFor();
        text("更多").waitFor();


        //点击更多
        do {
            log('点击id【礼物】');
            id("fbz").findOne().click()
            sleep(2000);
        } while (id("fbz").exists());
        console.verbose("开始等待text【土豪榜】出现！");text("土豪榜").waitFor();
        
        //进入操作列表
        log("点击text【本周土豪榜】");
        id("ir0").findOne().click();log("延迟1-2秒");sleep(random(1000, 2000));

        //进入主体循环
        v_visitUserHomepage(操作次数);

        //开始返回到主页
        do {
            // id("awa").findOne().click()
            back()
            sleep(1000, 2000)
        } while (!id("fbz").exists());
        log("开始while【loop1】循环");
        
        log("开始下滑到下一个主播");下滑();log("延迟五秒钟");sleep(5*1000);
    }
})();